package com.solomon.travel.common.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.solomon.travel.common.custom.mapper.CarCustomMapper;
import com.solomon.travel.common.custom.mapper.MotorcadeCustomMapper;
import com.solomon.travel.common.custom.po.MotorcadeCustom;
import com.solomon.travel.common.mapper.CarMapper;
import com.solomon.travel.common.mapper.MotorcadeMapper;
import com.solomon.travel.common.mapper.ProtocalMapper;
import com.solomon.travel.common.model.Page;
import com.solomon.travel.common.po.Car;
import com.solomon.travel.common.po.CarExample;
import com.solomon.travel.common.po.Motorcade;
import com.solomon.travel.common.po.MotorcadeExample;
import com.solomon.travel.common.po.Protocal;
import com.solomon.travel.common.po.ProtocalExample;
import com.solomon.travel.common.service.MotorcadeService;

@Service
public class MotorcadeServiceImpl implements MotorcadeService {
	
	@Resource
	private MotorcadeMapper motorcadeMapper;
	
	@Resource
	private CarMapper carMapper;
	
	@Resource
	private ProtocalMapper protocalMapper;

	@Override
	public int add(Motorcade motorcade) {
		return motorcadeMapper.insertSelective(motorcade);
	}

	@Override
	public int update(Motorcade motorcade) {
		return motorcadeMapper.updateByPrimaryKeySelective(motorcade);
	}

	@Override
	public int add(Car car) {
		return carMapper.insertSelective(car);
	}

	@Override
	public int update(Car car) {
		return carMapper.updateByPrimaryKeySelective(car);
	}

	@Override
	public MotorcadeCustom getByUser(String userId) {
		MotorcadeExample motorcadeExample = new MotorcadeExample();
		motorcadeExample.createCriteria().andUserIdEqualTo(userId);
		List<Motorcade> motorcades = motorcadeMapper.selectByExample(motorcadeExample);
		if (!motorcades.isEmpty()) {
			MotorcadeCustom motorcadeCustom = new MotorcadeCustom();
			Motorcade motorcade = motorcades.get(0);
			try {
				BeanUtils.copyProperties(motorcadeCustom, motorcade);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			CarExample carExample = new CarExample();
			carExample.createCriteria().andMotorcadeIdEqualTo(motorcade.getObjectId());
			ProtocalExample protocalExample = new ProtocalExample();
			protocalExample.createCriteria().andOrganizationIdEqualTo(motorcade.getObjectId());
			List<Protocal> protocals = protocalMapper.selectByExample(protocalExample);
			if (!protocals.isEmpty()) {
				motorcadeCustom.setProtocal(protocals.get(0));
			}
			motorcadeCustom.setCarModels(carMapper.selectByExample(carExample));
			return motorcadeCustom;
		}
		return null;
	}

	@Override
	public Motorcade getById(String id) {
		return motorcadeMapper.selectByPrimaryKey(id);
	}

	@Override
	public int add(Protocal protocal) {
		return protocalMapper.insertSelective(protocal);
	}

	@Override
	public int update(Protocal protocal) {
		return protocalMapper.updateByPrimaryKeySelective(protocal);
	}

	@Override
	public int count() {
		return motorcadeMapper.countByExample(null);
	}

	@Override
	public int countCar(String motorId) {
		CarExample example = new CarExample();
		example.createCriteria().andMotorcadeIdEqualTo(motorId);
		return carMapper.countByExample(example);
	}
	
	@Resource
	private MotorcadeCustomMapper motorcadeCustomMapper;
	
	@Resource
	private CarCustomMapper carCustomMapper;

	@Override
	public List<Motorcade> listMotor(Page page) {
		return motorcadeCustomMapper.listByPage(page);
	}

	@Override
	public List<Car> listCars(String motorId, Page page) {
		return carCustomMapper.listByPage(page, motorId);
	}

}
